package com.globallogic.javatrainings.sergiiivanov.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.Test;

import com.globallogic.javatrainings.sergiiivanov.model.Const;
import com.globallogic.javatrainings.sergiiivanov.model.Const.Discipline;
import com.globallogic.javatrainings.sergiiivanov.model.Course;
import com.globallogic.javatrainings.sergiiivanov.model.Student;
import com.globallogic.javatrainings.sergiiivanov.model.Teacher;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityFailure;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityWarning;
import com.globallogic.javatrainings.sergiiivanov.resources.ErrorCodes;

public class ScoreRecordTest extends BasicTest {

    /**
     * <pre>
     * 22  3.    Score records
     * 23  3.1.  Score record has score value and is linked to: course, lecture, teacher
     * 24  3.2.  All parameters are required for adding score record: course, lecture, teacher
     * </pre>
     */
    @Test
    public void test() throws UniversityWarning, UniversityFailure {
        Course c = new Course("c", null);
        c.addLecture("l", null, "");
        Student s = new Student("st1", "", "");
        Teacher t = new Teacher();

        try {
            s.addScore(null, Const.MAX_SCORE - 1, "");
            fail("Missed " + ErrorCodes.SCORE_COURSE_NULL);
        } catch (Exception e) {
            assertEquals(ErrorCodes.SCORE_COURSE_NULL, e.getMessage());
        }
        try {
            s.addScore(c.getLectureByIndex(0), Const.MAX_SCORE - 1, "");
            fail("Missed " + ErrorCodes.STUDENT_NOT_ENROLLED_TO + "c");
        } catch (Exception e) {
            assertEquals(ErrorCodes.STUDENT_NOT_ENROLLED_TO + "c", e.getMessage());
        }
        c.enrollStudent(s);

        try {
            s.addScore(c.getLectureByIndex(0), Const.MAX_SCORE - 1, "");
            fail("Missed " + ErrorCodes.SCORE_COURSE_BROKEN_DISCIPLINE);
        } catch (Exception e) {
            assertEquals(ErrorCodes.SCORE_COURSE_BROKEN_DISCIPLINE, e.getMessage());
        }
        c.setDiscipline(Discipline.Java6);

        try {
            s.addScore(c.getLectureByIndex(0), Const.MAX_SCORE - 1, "");
            fail("Missed " + ErrorCodes.SCORE_NO_TEACHER);
        } catch (Exception e) {
            assertEquals(ErrorCodes.SCORE_NO_TEACHER, e.getMessage());
        }
        c.setTeacher(t);

        try {
            s.addScore(c.getLectureByIndex(0), Const.MAX_SCORE + 1, "");
            fail("Missed " + ErrorCodes.SCORE_WRONG_VALUE);
        } catch (Exception e) {
            assertEquals(ErrorCodes.SCORE_WRONG_VALUE, e.getMessage());
        }
        for(int scoreV = Const.MIN_SCORE; scoreV <= Const.MAX_SCORE; scoreV++){
            s.addScore(c.getLectureByIndex(0), scoreV, "");
        }
    }

}
